package com.demo.action;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.demo.model.Marker;
import com.demo.service.MarkerService;
import com.opensymphony.xwork2.ActionSupport;

public class MarkerAction extends ActionSupport {

	protected final Log log = LogFactory.getLog(getClass());
	private MarkerService markerService;
	private String json = "";

	public MarkerService getMarkerService() {
		return markerService;
	}

	public void setMarkerService(MarkerService markerService) {
		this.markerService = markerService;
	}

	public String getJson() {
		return json;
	}

	public void setJson(String json) {
		this.json = json;
	}

	public String getAllMarkers() {
		List<Marker> markers = markerService.getAll();

		StringBuffer markerSB = new StringBuffer("");

		for (Marker item : markers) {
			if ((markerSB != null) && (!"".equals(markerSB.toString()))) {
				markerSB.append(",");
			}

			markerSB.append("{");

			if ((item.getId() != null) && (!"".equals(item.getId()))) {
				markerSB.append("\"id\":\"").append(item.getId()).append("\"");
			}

			if ((item.getLat() != null) && (!"".equals(item.getLat()))) {
				markerSB.append(",\"lat\":\"").append(item.getLat())
						.append("\"");
			}

			if ((item.getLng() != null) && (!"".equals(item.getLng()))) {
				markerSB.append(",\"lng\":\"").append(item.getLng())
						.append("\"");
			}

			if ((item.getMarker() != null) && (!"".equals(item.getMarker()))) {
				markerSB.append(",\"marker\":\"").append(item.getMarker())
						.append("\"");
			}

			if ((item.getListeners() != null)
					&& (!"".equals(item.getListeners()))) {
				markerSB.append(",\"listeners\":\"")
						.append(item.getListeners()).append("\"");
			}

			if ((item.getImg() != null) && (!"".equals(item.getImg()))) {
				markerSB.append(",\"img\":\"").append(item.getImg())
						.append("\"");
			}

			markerSB.append("}");
		}
//		markerSB.insert(0, "\"markers\" : [");
//		markerSB.append("]");

		this.json = ("[" + markerSB.toString() + "]");

		return "json";
	}

}
